﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage, IShoppingCart
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            LoadMenu();
        if (Session["ShoppingCart"] == null)
        {
            Session.Add("ShoppingCart", new MyShoppingCart());
            lblCartDetail.Text = String.Format("{0} món hàng", 0);
            lblTotal.Text = String.Format("{0:N0} đ", 0);
        }
        UpdateShoppingCart();
        LoadMedia();
    }

    private void LoadMedia()
    {
        DataTable dt = DataAccess.ExecuteQuery("Select * from Media where active = 1 order by NewID()");
        if(dt.Rows.Count > 0)
        {
            _lblPlayYoutubeVideo.Text = DataAccess.PlayVideoYoutube(dt.Rows[0]["Link"].ToString(), 223, 225);
            _lblDescription.Text = String.Format("<a class='PlayVideoYoutube' target='_blank' href='ProductDetail.aspx?id={1}'> {0} </a>", dt.Rows[0]["Description"], dt.Rows[0]["ProductID"]);
        }
    }
    private void LoadMenu()
    {
        string fileName = Server.MapPath("menu.xml");
        GenMenu.WriteXML_R(fileName);
        _lblMainMenu.Text = GenMenu.ReadXMLContent(fileName);
    }

    public void UpdateShoppingCart()
    {
        if (Session["ShoppingCart"] != null)
        {
            MyShoppingCart sc = Session["ShoppingCart"] as MyShoppingCart;

            lblCartDetail.Text = String.Format("{0} món hàng", sc.TotalItems);
            lblTotal.Text = String.Format("{0:N0} đ", sc.TotalPrice);
            UpdatePanel1.Update();
        }
    }
}
